/**
 *  Copyright � 2010 Digia Plc
 *  Copyright � 2010 Nokia Corporation
 *
 *  All rights reserved.
 *
 *  Nokia and Nokia Connecting People are registered trademarks of
 *  Nokia Corporation.
 *  Java and all Java-based marks are trademarks or registered
 *  trademarks of
 *  Sun Microsystems, Inc. Other product and company names
 *  mentioned herein may be
 *  trademarks or trade names of their respective owners.
 *
 *
 *  Subject to the conditions below, you may, without charge:
 *
 *  �  Use, copy, modify and/or merge copies of this software and
 *     associated documentation files (the "Software")
 *
 *  �  Publish, distribute, sub-licence and/or sell new software
 *     derived from or incorporating the Software.
 *
 *
 *  This file, unmodified, shall be included with all copies or
 *  substantial portions
 *  of the Software that are distributed in source code form.
 *
 *  The Software cannot constitute the primary value of any new
 *  software derived
 *  from or incorporating the Software.
 *
 *  Any person dealing with the Software shall not misrepresent
 *  the source of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 *  KIND, EXPRESS OR IMPLIED,
 *  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import Qt 4.7
import "javascript/sauna.js" as SaunaEngine
import "javascript/sql.js" as HCS
import "../colibri/javascripts/functions.js" as Functions
import "../colibri"
import "styles"

Item {
    id: structure

    property bool timerVisible: false
    property bool saunaOn: false;
    property CLStyle style: StyleHomeControlSystem {}
    property int saunaTemperature: parseInt(getSaunasRoomTemperature());
    property int saunaTimeLeft: 0;
    property bool ready: false;
    property int spacing: 10;

    /**
     * Returns room temperature in sauna.
     * @return Saunas room temperature
     */
    function getSaunasRoomTemperature() {
        HCS.getTheRoom("sauna");
        return HCS.theRoom.temperature;
    }

    /**
     * Stops sauna.
     */
    function stopSauna() {
        saunaTemperature = getSaunasRoomTemperature();
        HCS.setSaunaTemperature(getSaunasRoomTemperature());
        saunaOn = false;
        HCS.setSaunaOn(false);
        setTime.setValue(0);
    }

    /**
     * Starts sauna to destination temperature found from
     * setTemparature-slider.
     */
    function startSauna() {
        saunaTemperature = setTemperature.value;
        //saunaTimeLeft = setTime.value*60*1000;
        saunaOn = true;
        HCS.setSaunaOn(true);
        HCS.setSaunaTemperature(setTemperature.value);
    }

    /**
     * Starts sauna to destination temperature and length which
     * has been given to function in parameters.
     * @param temperature saunas temperature
     * @param length Time how long sauna is on.
     */
    function startSaunaWithParemeters(temperature,length) {
        if (length < setTime.value) return
        setTemperature.setValue(temperature);
        setTime.setValue(length);
        saunaOn = true;
        HCS.setSaunaOn(true);
        HCS.setSaunaTemperature(setTemperature.value);
    }

    onHeightChanged: {
        dial.width = structure.height*0.6
    }

    Component.onCompleted: {
        HCS.setDefaults();
    }

    width: 640
    height: 424

    MouseArea {
        anchors.fill: parent;
    }

    Rectangle{
        width:structure.width*0.955
        height:structure.height*0.924
        color:style.selectionColor
        anchors.centerIn:parent
        radius:6
    }



    Image {
        source: "images/control_bg_trans_420x275_png.png";
        anchors.fill: parent;
        smooth: true
        opacity:0.7
    }


    Timer {
        interval: 60000; running: true; repeat: true
        onTriggered: {
            /* Information is sauna ready is not needed in the moment
            //is sauna ready?
            if(!temperatureAnimation.running && saunaOn && !ready) {
                ready = true;
            } else ready = false;*/
            setTime.setValue(setTime.value - 1)
        }
    }

    Timer {
        interval: 500; running: true; repeat: true
        onTriggered: {
             //database listening
            if (HCS.isSaunaOn() && !saunaOn) startSauna()
            if (!HCS.isSaunaOn() && saunaOn) stopSauna();
        }
    }


    Behavior on saunaTemperature {
        SmoothedAnimation { id: temperatureAnimation; velocity: 2; }
    }

    Row {
        x: structure.width*0.02
        y: structure.height*0.05;

        Column {
            width: structure.width*0.42
            spacing: 10

            CLDial {
                id: dial
                width: structure.height*0.65
                value: saunaTemperature;
                needleImage: "../../homecontrol/images/pointer.png_dial2.png";
                backgroundImage:"../../homecontrol/images/sauna_meter_600x600.png";
                startValue: 0;
                endValue: 120;
                startAngle: -160//295
                endAngle: 80
                smooth: true
                anchors.horizontalCenter: parent.horizontalCenter
            }

            Row {
                spacing: structure.width*0.05
                anchors.horizontalCenter: parent.horizontalCenter

                Image {
                    id: saunaOnCircle
                    height: structure.height*0.1
                    width: height
                    source: "images/red_100x100_png.png"
                    smooth: true
                    states: [
                        State {
                            name: "saunaOn"; when: saunaOn;
                            PropertyChanges{target: saunaOnCircle; source: "images/green_100x100_png.png"}
                        }
                    ]
                }

                Text {text: if (saunaOn) "Sauna is on"; else "Sauna is off"; font.pointSize: 0.001 + structure.height*0.05; color: structure.style.textColor}
            }
        }

        Column {
            spacing: 20
            y: structure.height*0.12
            Item {
                id: saunaControls
                width: structure.width/3;
                height: 220;
                Column {
                    spacing: structure.height*0.05
                    Text {text: "Temperature: " + setTemperature.value + " \u00BAC"; font.pointSize: 0.001+structure.height*0.06; color: structure.style.textColor}
                    CLSlider{
                        id: setTemperature;
                        height: structure.height*0.11;
                        width: structure.width*0.52;
                        style: structure.style;
                        minimum: 30
                        maximum: 120
                        borderColor: "white";
                        borderWidth: 3
                        onValueChanged: if(saunaOn) saunaTemperature = setTemperature.value;
                        Component.onCompleted: setValue(70);
                    }

                    Text {
                        text: "Time left: " + (parseInt(setTime.value/60) + " h" + " " +(setTime.value % 60)) +  " min"; font.pointSize: 0.001+structure.height*0.06
                        color: structure.style.textColor
                    }

                    CLSlider{
                        id: setTime;
                        minimum: 0;
                        maximum: 180;
                        height: structure.height*0.11;
                        width: structure.width*0.52;
                        style: structure.style; borderColor: "white";
                        borderWidth: 3;
                        Component.onCompleted: setValue(0);
                        onValueChanged: {
                            if (value <= 0) stopSauna()
                            else if (!saunaOn) startSauna();
                        }
                    }
                }
            }
        }
    }
}

